package StudyCode.summary.myfile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetFullFileName {
	private static final char UNIX_SEPARATOR = '/';
	private static final char WINDOWS_SEPARATOR = '\\';

	public static void main(String[] argv) {
		String filename = "E:\\迅雷下载\\test1.txt";
		System.out.println("文件 " + filename + " 的文件名称是： " + getName(filename));
	}

	private static String getName(String filename) {
		if (filename == null) {
			return null;
		}
		int index = indexOfLastSeparator(filename);
		return filename.substring(index + 1);
	}

	private static int indexOfLastSeparator(String filename) {
		if (filename == null) {
			return -1;
		}
		int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
		int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
		return Math.max(lastUnixPos, lastWindowsPos);
	}
}
